<?php

class MString {

    public static function truncate($string, $maxLength = 100, $replacement = '...', $truncAtSpace = false) {
        $maxLength -= strlen($replacement);
        $stringLength = strlen($string);

        if ($stringLength <= $maxLength)
            return $string;

        if ($truncAtSpace && ($spacePosition = strrpos($string, ' ', $maxLength - $stringLength)))
            $maxLength = $spacePosition;

        return substr_replace($string, $replacement, $maxLength);
    }

}

?>
